package com.alibaba.wallstreet.uitls;

import java.text.DecimalFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.alibaba.citrus.util.i18n.LocaleUtil;
import com.alibaba.wallstreet.uitls.ResourceManagerUtil.ResourceName;
import com.alibaba.wallstreet.uitls.date.DateFormatPatterns;
import com.alibaba.wallstreet.uitls.date.SuperDate;

public class VelocityUtil extends I18NUtil {

    private static final String DEFAULT_FORMAT_PATTERN = "#,##0.00";

    public static String formatDate(Date date, String pattern) {
        if (StringUtils.isEmpty(pattern)) pattern = DateFormatPatterns.DATE_PATTERN_DEFAULT.getPattern();
        if (date == null) {
            return "DATE NULL";
        }
        return SuperDate.format(date, DateFormatPatterns.valueOf(pattern));
    }

    public static String formatNumber(Number number, String pattern) {
        if (number == null) return null;
        if (StringUtils.isEmpty(pattern)) {
            pattern = DEFAULT_FORMAT_PATTERN;
        }
        DecimalFormat format = new DecimalFormat(pattern);
        return format.format(number);

    }

    public static String getLanguage() {
        return LocaleUtil.getContext().getLocale().getLanguage();
    }

    public static String formatNumber(Number number, int precision) {
        StringBuffer pattern = new StringBuffer("#,##0");
        if (precision > 0) {
            pattern = pattern.append(".0000000".substring(0, 1 + precision));
        }
        return formatNumber(number, pattern.toString());
    }

    public static String formatNumber(Number number) {
        return formatNumber(number, DEFAULT_FORMAT_PATTERN);
    }

    public static long getSystemTime() {
        return System.currentTimeMillis();
    }

    public static String getResItem(String resType, String resName) {
        return ResourceManagerUtil.lookup(ResourceName.valueOf(resType), resName);
    }

    public static String getI18N(String key) {
        return ResourceManagerUtil.getI18nMessage(key);
    }

}
